package com.qkbb.admin.kuibu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qkbb.admin.kuibu.R;
import com.qkbb.admin.kuibu.qkbb.JavaBean.Game;
import com.qkbb.admin.kuibu.qkbb.funcation.ImageDownloadSDCardCacheHelper;
import com.qkbb.admin.kuibu.qkbb.funcation.OSShelp;

import java.util.List;

/**
 * Created by admin on 2016/2/19.
 */
public class JoinListAdapter extends BaseAdapter {
    Context context;
    private List<Game> list;
    private OSShelp osShelp;
    private ViewHold viewhold;
    private ImageDownloadSDCardCacheHelper imageDownloadSDCardCacheHelper;

    public JoinListAdapter(Context context) {
        this.context=context;
    }

    public List<Game> getList() {
        return list;
    }

    public void setList(List<Game> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        osShelp=new OSShelp(context);
        if (convertView==null){
            viewhold=new ViewHold();
            convertView= LayoutInflater.from(context).inflate(R.layout.joinlistitem,null);
            viewhold.imageView= (ImageView) convertView.findViewById(R.id.joinlist_item_imageview);
            viewhold.peopleview= (TextView) convertView.findViewById(R.id.joinlist_item_renshu);
            viewhold.timeview= (TextView) convertView.findViewById(R.id.joinlist_item_time);
            viewhold.titleview= (TextView) convertView.findViewById(R.id.joinlist_item_title);
            viewhold.scheduleview= (TextView) convertView.findViewById(R.id.joinlist_item_jindu);
            viewhold.descriptionview= (TextView) convertView.findViewById( R.id.joinlist_item_text_description);
            convertView.setTag(viewhold);
        }
        else{
            viewhold= (ViewHold) convertView.getTag();
        }
        viewhold.titleview.setText(list.get(position).getRoadname());
        viewhold.descriptionview.setText(list.get(position).getDescription());
        viewhold.peopleview.setText(list.get(position).getNumber_of_participants() + "");
        viewhold.timeview.setText(list.get(position).getStarttime());

        setImage(viewhold.imageView, list.get(position).getPictureurl());
        if (list.get(position).getRoadsteplength() != 0) {
            viewhold.scheduleview.setText(list.get(position).getTotal_steps()/list.get(position).getRoadsteplength()+"");
        }




        return convertView;
    }
    public void setImage(final ImageView view,String imagename) {
//        String uri=osShelp.getBitmap(imagename);
//        Picasso.with(context).load(uri).into(view);
        String uri = osShelp.getBitmap(imagename);
        imageDownloadSDCardCacheHelper = new ImageDownloadSDCardCacheHelper();
        imageDownloadSDCardCacheHelper.myDownloadImageSDCardCache(context, uri, view, imagename);
    }
    class ViewHold{
        ImageView imageView;
        TextView titleview;
        TextView timeview;
        TextView peopleview;
        TextView scheduleview;
        TextView descriptionview;
    }
}
